<?php 
$pPageIsPublic = false;
include '../_common.php';
$_SESSION["m1"]="suggestion";
$_SESSION["m2"]="bydivstyle";

if ($_POST['divingstyleId']) {
	fetchDivingCenters($_POST['divingstyleId']);
	return;
}

if ($_POST['action'] == 'submit') {
	$divcenters = $_POST['divcenter'];
	$suggest    = $_POST['suggest'];
	$divstyleId = $_POST['divstyleId'];
	
	foreach($divcenters as $key => $divcenter) {
		$tmp = new DivCenterDivStyle();
		if (! $tmp->loadByFilter('divingstyle.divingstyleId='. $divstyleId 
							  .' AND '. 'divingcenter.divingcenterId='. $divcenter)) {
			continue;
		} else {
			$tmp ->suggest = $suggest[$key];
			$tmp ->update('suggest');
		}
	}
	return;
}

?>
<?php include("header.php"); ?>   
<script type="text/javascript">
$().ready(function() {

	
	$('input:checkbox.all').live('click', function() {
		$('ul#listin').find('input:checkbox').attr('checked', this.checked);
	});
	
	$('input[type="button"]').live('click', function() {
		var ids = "";
		var values = "";
		
		$('input:checkbox.check').each(function(){
			ids += "&divcenter[]=" + this.name;
			values += "&suggest[]=" + (this.checked ? "1" : "0");         
		});
		
		$.ajax({
			type      : "POST",
			data   	  : "action=submit&divstyleId=" + $('select[name="divingstyleId"]').val() + ids + values,
			success   : function(response) {
							Notify("Suggestion for Diving Style has been updated.", 0);	
						}
		});
	});
});

function loadList(id) {
	$.ajax({
		type      : "POST",
		data   	  : "divingstyleId=" + id,
		success   : function(response) {
						$('div#list').html(response);	
					}
	});
}

</script> 
<?php include("sidebar.php"); ?>
<ul id="select-nav"   >
  <li class="noborder"  ><strong> Select a Diving Style:</strong></li>
  <li class="noborder" >
<?php
	$tmp = new DivCenterDivStyle();
	$tmp ->addOrder($tmp ->gTable().'.orderId ASC');
	$tmp ->addWhere('divingstyle.status = "Live" AND divingcenter.status = "Live"');
	$tmp ->addGroup('divingstyle.title');
	$tmp ->loadList();
	$tmp ->qSelect3('divingstyleId', 'getDivingStyle()', '', '--- select a diving style ---', 'width:210px;', 'onchange="return this.value ? loadList(this.value) : location.reload(true);"', 'getDivingStyleId()');
	unset($tmp);
?>  
  </li>
</ul>

<div id="list">
	<h4>Select a Diving Style</h4> 
</div>
<div class="clear"></div>    
<?php include("footer.php"); ?>

<?php
	function fetchDivingCenters($divstyleId){
		$tmp = new DivCenterDivStyle();
		
		$sql = "SELECT mwm_divcenterdivstyle.*, divingcenter.date_create as divingcenter_date_create, divingcenter.date_update as divingcenter_date_update, divingcenter.id_person_create as divingcenter_id_person_create, divingcenter.title as divingcenter_title, divingcenter.subTitle as divingcenter_subTitle, divingcenter.description as divingcenter_description, divingcenter.bgImage as divingcenter_bgImage, divingcenter.status as divingcenter_status, divingcenter.seo as divingcenter_seo, divingcenter.suggest as divingcenter_suggest, divingcenter.regionId as divingcenter_regionId, divingstyle.date_create as divingstyle_date_create, divingstyle.date_update as divingstyle_date_update, divingstyle.id_person_create as divingstyle_id_person_create, divingstyle.title as divingstyle_title, divingstyle.subTitle as divingstyle_subTitle, divingstyle.description as divingstyle_description, divingstyle.bgImage as divingstyle_bgImage, divingstyle.status as divingstyle_status, divingstyle.seo as divingstyle_seo, divingstyle.bnImage as divingstyle_bnImage 
				FROM mwm_divcenterdivstyle LEFT JOIN mwm_divingcenter as divingcenter ON divingcenter.divingcenterId=mwm_divcenterdivstyle.divingcenterId LEFT JOIN mwm_divingstyle as divingstyle ON divingstyle.divingstyleId=mwm_divcenterdivstyle.divingstyleId 
				LEFT JOIN mwm_region as region ON region.regionId=divingcenter.regionId LEFT JOIN mwm_country as country ON country.countryId=region.countryId 
				WHERE divingstyle.divingstyleId = {$divstyleId} GROUP BY divingcenter.title ORDER BY country.title, region.title, divingcenter.title ASC";		
		
		//$tmp ->addWhere('divingstyle.divingstyleId = '. $divstyleId);
		//$tmp ->addOrder($tmp ->gTable().'.orderId ASC');
		//$tmp ->addGroup('divingcenter.title');
		
		//$tmp =& TznDb::query($sql);
		$tmp ->loadList(true, $sql);
		if (! $tmp ->rMore()) {	
			echo "<h4>No Result ...!</h4>";
		} else { ?>
	<h2>Suggestions for Diving Style</h2>
	<table id="gradient-style">
		<thead>
			<tr>
			 <th width="48%" >Diving Center</th>
			 <th width="20%" >Country</th>
			 <th width="20%" >Region</th>
			 <th width="12%" >Suggest <input type="checkbox" class="all"   > </th>
			</tr>
		</thead>
	</table>
	<ul id="listin" >
<?php 		
			while ($divcenter = $tmp ->rNext()) {
				$divcenter ->divingcenter ->region ->load();
?>
		<li><table id="gradient-style" >
		  <tr>
		    <td width="48%"  ><?php echo $divcenter ->divingcenter ->title   ?></td>
		    <td width="20%"  ><?php echo $divcenter ->divingcenter ->region ->country ->title   ?></td>
		    <td width="20%"  ><?php echo $divcenter ->divingcenter ->region ->title   ?></td>
		    <td width="12%" style="text-align: right; padding-right: 9px;"   >
		    <input class="check"  type="checkbox" id="divcenter[]" name="<?php echo $divcenter ->divingcenter ->id  ?>"   <?php echo $divcenter ->suggest ? 'checked="checked"' : ''?> /></td>
		  </tr>
		</table></li>			
<?php 				
			} ?>
	</ul>
	<div style="padding-top: 8px;text-align: right; letter-spacing: 6px;" >
		<input type="button" value="Save" name="btnSave"   />
	</div>			
<?php 			
		}
	}
?>
